{% extends 'base_tabler.html' %} {% load static %} {% block content %}
<div class="container-xl">
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">用户管理</h2>
      </div>
      <div class="col-auto ms-auto d-print-none">
        <a href="{% url 'admin_dashboard' %}" class="btn btn-secondary"> 返回仪表盘 </a>
      </div>
    </div>
  </div>

  {% if messages %}
  <div class="mt-3">
    {% for message in messages %}
    <div class="alert alert-{{ message.tags }}">{{ message }}</div>
    {% endfor %}
  </div>
  {% endif %}

  <div class="card mt-3">
    <div class="card-header">
      <h3 class="card-title">用户列表</h3>
    </div>
    <div class="table-responsive">
      <table class="table table-vcenter card-table">
        <thead>
          <tr>
            <th>用户名</th>
            <th>邮箱</th>
            <th>注册时间</th>
            <th>用户组</th>
            <th class="w-1">操作</th>
          </tr>
        </thead>
        <tbody>
          {% for user_item in users %}
          <tr>
            <td>{{ user_item.username }}</td>
            <td>{{ user_item.email|default:"-" }}</td>
            <td>{{ user_item.date_joined|date:"Y-m-d H:i" }}</td>
            <td>
              {% if user_item.groups.all %} {% for group in user_item.groups.all %}
              <span class="badge bg-blue">{{ group.name }}</span>
              {% endfor %} {% else %}
              <span class="badge bg-secondary">普通用户</span>
              {% endif %}
            </td>
            <td>
              <div class="btn-group">
                <form method="post" style="display: inline">
                  {% csrf_token %}
                  <input type="hidden" name="user_id" value="{{ user_item.id }}" />
                  {% if "Admin" in user_item.groups.all|stringformat:"s" %}
                  <button type="submit" name="action" value="remove_admin" class="btn btn-warning btn-sm">
                    移除管理员
                  </button>
                  {% else %}
                  <button type="submit" name="action" value="make_admin" class="btn btn-success btn-sm">
                    设为管理员
                  </button>
                  {% endif %} {% if user_item != user %}
                  <button
                    type="submit"
                    name="action"
                    value="delete"
                    class="btn btn-danger btn-sm"
                    onclick="return confirm('确定要删除该用户吗？')"
                  >
                    删除
                  </button>
                  {% endif %}
                </form>
              </div>
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% if users.paginator.num_pages > 1 %}
    <div class="card-footer d-flex align-items-center">
      <p class="m-0 text-muted">第 {{ users.number }} 页，共 {{ users.paginator.num_pages }} 页</p>
      <ul class="pagination m-0 ms-auto">
        {% if users.has_previous %}
        <li class="page-item">
          <a class="page-link" href="?page={{ users.previous_page_number }}">
            <!-- Download SVG icon from http://tabler-icons.io/i/chevron-left -->
            <svg
              xmlns="http://www.w3.org/2000/svg"
              class="icon"
              width="24"
              height="24"
              viewBox="0 0 24 24"
              stroke-width="2"
              stroke="currentColor"
              fill="none"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <path stroke="none" d="M0 0h24v24H0z" fill="none" />
              <path d="M15 6l-6 6l6 6" />
            </svg>
            上一页
          </a>
        </li>
        {% endif %} {% if users.has_next %}
        <li class="page-item">
          <a class="page-link" href="?page={{ users.next_page_number }}">
            下一页
            <!-- Download SVG icon from http://tabler-icons.io/i/chevron-right -->
            <svg
              xmlns="http://www.w3.org/2000/svg"
              class="icon"
              width="24"
              height="24"
              viewBox="0 0 24 24"
              stroke-width="2"
              stroke="currentColor"
              fill="none"
              stroke-linecap="round"
              stroke-linejoin="round"
            >
              <path stroke="none" d="M0 0h24v24H0z" fill="none" />
              <path d="M9 6l6 6l-6 6" />
            </svg>
          </a>
        </li>
        {% endif %}
      </ul>
    </div>
    {% endif %}
  </div>
</div>
{% endblock %}
